package com.example.springbootdemo.util;

import org.redisson.Redisson;
import org.redisson.api.RRateLimiter;
import org.redisson.api.RateIntervalUnit;
import org.redisson.api.RateType;

/**
 * @author 84246
 */
public class RedisSonUtils {

    private static Redisson redisson = null;

    static {
        redisson = ApplicationContextUtil.getBean(Redisson.class);
    }

    public static long rateLimiter(String key, int count, int time, RateType rateType) {
        RRateLimiter rateLimiter = redisson.getRateLimiter(key);
        rateLimiter.trySetRate(rateType, count, time, RateIntervalUnit.SECONDS);
        if (rateLimiter.tryAcquire()) {
            return 1;
        } else {
            return 0;
        }
    }
}
